#pragma region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#pragma endregion

#pragma once

#define MAX_MIXER_STREAMS  16

class AllocatorPresenter;

MIDL_INTERFACE("{0C3F3184-18D5-4ad5-BBA0-B7606A05A3A8}")
IOSHMixerPlugin : public IUnknown
{
public:
	virtual STDMETHODIMP Initialize() = 0;
};

class __declspec(uuid("{C4FFFC3B-EB36-474d-9F57-A80F0144ABCC}")) OSHMixerPlugin;
class OSHMixerPlugin :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<OSHMixerPlugin, &__uuidof(OSHMixerPlugin)>,
	public IOSHMixerPlugin,
	public IDistributorNotify,
	public OSHSourceEventSink
{
private:
	OSHSourceConnector* _oshSourceConnector;
	VMRStateController _vmrStateController;
	CSimpleArray<CComPtr<IBaseFilter>> _removedFilters;
	CSimpleArray<CComPtr<IBaseFilter>> _insertedFilters;

private:
	enum {
		INSERT_OSHSOURCE_TRACE_LEVEL = 1,
		DefaultTransparentColor = RGB(0xFF, 0x00, 0xFF),
	};

	IFilterGraph* _filterGraph;	// Do not AddRef this pointer to prevent circular reference count.
	bool _insideNotifyGraphChange;
	bool _insideSetSyncSource;
	bool _insideStop;
	bool _insidePause;

	bool _graphIsRunning;
	//AllocatorPresenter* _oshAP;

	HRESULT SetAlphaBitmap(OSHLibrary::IBitmap* bitmap);
	template<typename IVMRMixerBitmapT, typename VMRAlphaBitmapT, REFGUID VMRGUID>
	HRESULT SetAlphaBitmap(IFilterGraph* filterGraph, HDC hdc, CSize bitmapSize, COLORREF transparentColor);
	COLORREF FindTransparentColor(BYTE* bitmapPixels, size_t bitmapSize);

	HRESULT GetBitmapInfo(OSHLibrary::IBitmap* oshBitmap, BITMAPINFO** bitmapInfo, BYTE** bitmapPixels);
	HRESULT CreateBitmap(HDC hdc, IN BITMAPINFO* bitmapInfo, IN BYTE* bitmapPixels, OUT HBITMAP* hBitmap);

	HRESULT RemoveFilters();
	HRESULT ReturnRemovedFilters();
	HRESULT InsertFilters();
	HRESULT RemoveInsertedFilters();
	HRESULT RemoveUnconnectedInsertedFilters();

	HRESULT EnsureVMRMode(IFilterGraph* filterGraph);
	template<typename IVMRFilterConfigT, REFGUID VMRGUID>
	HRESULT EnsureVMRMode(IFilterGraph* filterGraph);
	HRESULT GetVMRPinToConnect(IFilterGraph* filterGraph, IPin** pin);
	HRESULT FindVMRFilter(IFilterGraph* filterGraph, IBaseFilter** vmrFilter);

	void OnShow(OSHLibrary::IBitmap* ppBitmap);
	void OnHide();
	void OnRefresh(OSHLibrary::IBitmap* ppBitmap);

	void Refresh(OSHLibrary::IBitmap* oshBitmap);

public:
	OSHMixerPlugin();
	virtual ~OSHMixerPlugin();

	DECLARE_AGGREGATABLE(OSHMixerPlugin)

	BEGIN_COM_MAP(OSHMixerPlugin)
		COM_INTERFACE_ENTRY(IOSHMixerPlugin)
		COM_INTERFACE_ENTRY(IDistributorNotify)
	END_COM_MAP()

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	DECLARE_REGISTRY_RESOURCEID(IDR_OSHMixerPlugin)

	HRESULT FinalConstruct();
	void FinalRelease();

	//
	// IOSHMixerPlugin
	//
	STDMETHOD(Initialize)();

	//
	// IDistributorNotify
	//
    STDMETHOD(Stop)();
    STDMETHOD(Pause)();
    STDMETHOD(Run)(REFERENCE_TIME tStart);
    STDMETHOD(SetSyncSource)(/* [in] */ IReferenceClock* pClock);
    STDMETHOD(NotifyGraphChange)();

};

OBJECT_ENTRY_AUTO(__uuidof(OSHMixerPlugin), OSHMixerPlugin)
